package com.yanqu.road.dataHelper.action;

import com.yanqu.road.dataHelper.action.excel.UnionActivityServerMove;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ActivityMgr;
import com.yanqu.road.dataHelper.manager.BaseMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.servercenter.CrossServerInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.union.UnionInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.*;

public class UnionActivityMove2 extends actionBase {
    protected static Logger logger = LogManager.getLogger(UnionActivityMove2.class.getName());
    public void go(List<Long> serverList) {
        ExecutorService loginExecutors = Executors.newFixedThreadPool(10);
        ActivityMgr activityMgr = BaseMgr.getMgr(ActivityMgr.class);
        Map<Long, ServerInfo> serverInfoMap = ServerMgr.getServerInfoList();
        activityMgr.initAllRunningActivity();
        ServerMgr serverMgr = new ServerMgr();
        List<Future<Boolean>> futureList = new ArrayList<>();
        List<CrossServerInfo> crossServerInfoList = new ServerMgr().getCrossActivityServerDb(101);
        for (Long serverId : serverList) {
            Future<Boolean> future = loginExecutors.submit(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    return moveServerData(activityMgr, serverInfoMap, serverMgr, crossServerInfoList, serverId);
                }
            });
            futureList.add(future);
        }

        for (Future<Boolean> future : futureList) {
            try {
                Boolean success = future.get();
                if (!success) {
                    logger.error("=====  error ======,");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        DataMgr.makeExcelData2Txt("");
        DataMgr.outText("111");

    }

    private boolean moveServerData(ActivityMgr activityMgr, Map<Long, ServerInfo> serverInfoMap, ServerMgr serverMgr, List<CrossServerInfo> crossServerInfoList, Long serverId) {
        try {
            ServerInfo serverInfo = serverInfoMap.get(serverId);
            if (serverInfo != null) {
                List<ActivityInfo> activityInfoList = activityMgr.getServerAllRunningActivity(serverId);
                UnionActivityServerMove serverMove = new UnionActivityServerMove(new GameServer(serverInfo));
                List<UserActivityConditionData> allConditionData = new ArrayList<>();
                // 获取玩家活动数据
                for (ActivityInfo info : activityInfoList) {
                    int activityId = info.getActivityId();
                    Set<Integer> conditionTypeSet = activityMgr.getConditionInfoMap(activityId);
                    for (Integer conditionType : conditionTypeSet) {
                        if (!unionRankType().contains(conditionType)) {
                            continue;
                        }
                        if (info.getType() == eActivityType.CommonActivity.getValue() || info.getType() == eActivityType.CrossCommonActivity.getValue()) {
                            List<UserActivityConditionData> dataList = serverMove.getCommonUserActivityConditionData(activityId, conditionType);
                            allConditionData.addAll(dataList);
                        }else {
                            Map<Long, UserActivityConditionData> dataMap = serverMove.getUserActivityConditionData(activityId, conditionType);
                            allConditionData.addAll(dataMap.values());
                        }
                    }
                }
                for (UserActivityConditionData data : allConditionData) {
                    ActivityInfo info = ActivityMgr.getActivityInfo(data.getActivityId());
                    // 插入到 跨服
                   boolean success =  serverMgr.addUserConditionData(data, getCrossServerInfo(info.getCrossId(), crossServerInfoList));
                    if (!success) {
                        logger.info("move error. serverId: {},userId :{}, activityId: {}", serverId, data.getUserId(), data.getActivityId());
                        return false;
                    }
                }

                for (ActivityInfo info : activityInfoList) {
                    int activityId = info.getActivityId();
                    Map<String, UnionInfo> unionInfoMap = new HashMap<>();
                    List<UnionActivityConditionData> dataList = serverMove.getUnionConditionData(activityId);
                    for (UnionActivityConditionData data : dataList) {
                        if(((info.getType() == eActivityType.UnionRankActivity.getValue() && info.getChildType() ==2) ||
                                (info.getType() == eActivityType.CrossUnionActivity.getValue() && info.getChildType() == 3))) {
                            UnionInfo unionInfo = unionInfoMap.get(data.getUnionUid());
                            if (unionInfo == null) {
                                unionInfo = serverMove.getUnionInfo(data.getUnionUid());
                            }
                            if (unionInfo != null) {
                                unionInfoMap.put(unionInfo.getUnionUid(), unionInfo);
                                if (data.getActivityId() == unionInfo.getRankActivityId()) {
                                    long rankAddExp = unionInfo.getRankAddExp();
                                    data.setExtraValue(BigInteger.valueOf(rankAddExp));
                                }
                            }
                        }
                        boolean success = serverMgr.addUnionConditionData(data, getCrossServerInfo(info.getCrossId(), crossServerInfoList));
                        if (!success) {
                            logger.info("move error. serverId: {},unionId :{}, activityId: {}", serverId, data.getUnionUid(), data.getActivityId());
                            return false;
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("move error. serverId: {}",serverId, e);
            return false;
        }
        return true;
    }

    private CrossServerInfo getCrossServerInfo(Long crossId, List<CrossServerInfo> crossServerInfoList) {
        for (CrossServerInfo info : crossServerInfoList) {
            if (info.getCrossServerId() == crossId) {
                return info;
            }
        }
        return null;
    }

    private Set<Integer> unionRankType() {
        Set<Integer> set = new HashSet<>();
        set.add(0);
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(39);
        set.add(40);
        set.add(41);
        set.add(61);
        set.add(62);
        set.add(65);
        set.add(69);
        set.add(88);
        set.add(90);
        set.add(1014);
        set.add(1017);
        set.add(1027);
        set.add(1029);
        set.add(1030);
        set.add(1085);
        return set;
    }
}
